#include "TestRadioButton.h"
#include <QRadioButton>
#include <QButtonGroup>
#include<QVBoxLayout>
#include<QDebug>
#include<QPushButton>
#include<QLabel>
#include<QGroupBox>
TestRadioButton::TestRadioButton(QWidget *parent) : QWidget(parent)
{
    QVBoxLayout *vlayout =new QVBoxLayout(this);

    //1.简单使用
    // 创建按钮
    QRadioButton *radio1 = new QRadioButton("Option 1");
    QRadioButton *radio2 = new QRadioButton("Option 2");
    QRadioButton *radio3 = new QRadioButton("Option 3");

    // 分组管理（确保互斥）
    QButtonGroup *group = new QButtonGroup(this);
    group->addButton(radio1, 1);  // ID=1
    group->addButton(radio2, 2);  // ID=2
    group->addButton(radio3, 3);  // ID=3

    // 连接信号（推荐使用 toggled 或 QButtonGroup 的信号）
    connect(radio1, &QRadioButton::toggled, [](bool checked) {
        if (checked) qDebug() << "Option 1 selected!";
    });

    // 通过QButtonGroup获取选中ID
    connect(group, QOverload<int>::of(&QButtonGroup::buttonClicked),
            [](int id) { qDebug() << "Selected ID:" << id; });

    // 获取当前选中
    QPushButton *submit = new QPushButton("确认");
    connect(submit, &QPushButton::clicked, [=](){
        qDebug() << "当前选择:" << group->checkedButton()->text();
    });

    vlayout->addWidget(radio1);
    vlayout->addWidget(radio2);
    vlayout->addWidget(radio3);
    vlayout->addWidget(submit);

    //2 设置
    QGroupBox *groupBox = new QGroupBox("设置选项", this);
    QGridLayout *layout_groupBox = new QGridLayout(groupBox);
     vlayout->addWidget(groupBox);
    //2.1语言选择
    QLabel *langLabel = new QLabel("语言:");
    QButtonGroup *langGroup = new QButtonGroup(groupBox);
    QRadioButton *langCN = new QRadioButton("中文");
    QRadioButton *langEN = new QRadioButton("英文");
    langGroup->addButton(langCN, 0); // 0代表中文
    langGroup->addButton(langEN, 1); // 1代表英文
    layout_groupBox->addWidget(langLabel, 0, 0);
    layout_groupBox->addWidget(langCN, 0, 1);
    layout_groupBox->addWidget(langEN, 0, 2);

    //2.3 主题选择
    QLabel *themeLabel = new QLabel("主题:");
    QButtonGroup *themeGroup = new QButtonGroup(groupBox);
    QRadioButton *themeLight = new QRadioButton("亮色");
    QRadioButton *themeDark = new QRadioButton("暗色");
    themeGroup->addButton(themeLight, 0); // 0代表亮色
    themeGroup->addButton(themeDark, 1);  // 1代表暗色
    layout_groupBox->addWidget(themeLabel, 1, 0);
    layout_groupBox->addWidget(themeLight, 1, 1);
    layout_groupBox->addWidget(themeDark, 1, 2);

    //2.4 单位制式
    QLabel *unitLabel = new QLabel("单位制式:");
    QButtonGroup *unitGroup = new QButtonGroup(groupBox);
    QRadioButton *unitMetric = new QRadioButton("公制");
    QRadioButton *unitImperial = new QRadioButton("英制");
    unitGroup->addButton(unitMetric, 0); // 0代表公制
    unitGroup->addButton(unitImperial, 1); // 1代表英制
    layout_groupBox->addWidget(unitLabel, 2, 0);
    layout_groupBox->addWidget(unitMetric, 2, 1);
    layout_groupBox->addWidget(unitImperial, 2, 2);

    //3  表单填写组
    QGroupBox *groupBox_3 = new QGroupBox("表单填写", this);
    QGridLayout *layout_groupBox_3 = new QGridLayout(groupBox_3);
    vlayout->addWidget(groupBox_3);

    // 性别选择
    QLabel *genderLabel = new QLabel("性别:");
    QButtonGroup *genderGroup = new QButtonGroup(groupBox_3);
    QRadioButton *male = new QRadioButton("男");
    QRadioButton *female = new QRadioButton("女");
    QRadioButton *other = new QRadioButton("其他");
    genderGroup->addButton(male, 0);
    genderGroup->addButton(female, 1);
    genderGroup->addButton(other, 2);

    // 支付方式
    QLabel *paymentLabel = new QLabel("支付方式:");
    QButtonGroup *paymentGroup = new QButtonGroup(groupBox_3);
    QRadioButton *cash = new QRadioButton("现金");
    QRadioButton *card = new QRadioButton("信用卡");
    QRadioButton *alipay = new QRadioButton("支付宝");
    QRadioButton *wechat = new QRadioButton("微信支付");
    paymentGroup->addButton(cash, 0);
    paymentGroup->addButton(card, 1);
    paymentGroup->addButton(alipay, 2);
    paymentGroup->addButton(wechat, 3);

    // 提交按钮
    QPushButton *submitButton = new QPushButton("提交表单");
    connect(submitButton, &QPushButton::clicked, this, &TestRadioButton::submitForm);

    // 添加到布局
    layout_groupBox_3->addWidget(genderLabel, 0, 0);
    layout_groupBox_3->addWidget(male, 0, 1);
    layout_groupBox_3->addWidget(female, 0, 2);
    layout_groupBox_3->addWidget(other, 0, 3);

    layout_groupBox_3->addWidget(paymentLabel, 1, 0);
    layout_groupBox_3->addWidget(cash, 1, 1);
    layout_groupBox_3->addWidget(card, 1, 2);
    layout_groupBox_3->addWidget(alipay, 1, 3);
    layout_groupBox_3->addWidget(wechat, 1, 4);

    layout_groupBox_3->addWidget(submitButton, 2, 0, 1, 5, Qt::AlignCenter);
}

void TestRadioButton::submitForm()
{

}
